﻿using UnityEngine;
using System.Collections;

public class AutoRotate : MonoBehaviour
{
    public float speed = 10;
    public ChangeMode changeMode;
    private Quaternion targetQuat;
    private Vector3 targetVec;
    private const float minDis = 0.1f;
    private static bool playing = false;
    // Use this for initialization
    void Start()
    {
        // StartRotate(Vector3.forward);
        enabled = false;
    }

    // Update is called once per frame
    void Update()
    {
        transform.localRotation = Quaternion.Slerp(transform.localRotation, targetQuat, speed * Time.deltaTime);

        if (Vector3.Distance(transform.localEulerAngles, targetVec) <= minDis)
        {
            transform.localEulerAngles = targetVec;
            changeMode.ReCalcCubePos();
            playing = false;
            enabled = false;
        }
    }

    public bool StartRotate(Vector3 dir)
    {
        if (playing == true)
        {
            return false;
        }

        Quaternion oldLocalRotation = transform.localRotation;
        transform.Rotate(dir * 90);
        targetVec = transform.localEulerAngles;
        targetQuat = transform.localRotation;
        transform.localRotation = oldLocalRotation;
        playing = true;
        enabled = true;
        return true;
    }
}
